﻿using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using Com.Ompchina.Lib.DBHelper;
using Model.AuthModel;
using Model.Base;
using IDAL.AuthModel;
namespace SQLServerDAL.AuthModel
{
    /// <summary>
    /// 通用组件，权限模型，数据层，角色
    /// </summary>
    public class RolesDAL : DalBase, IRoles
    {
        public RolesDAL() 
        {
            Schema = "am.";
            TableName = "Roles";
            ViewTableName = "V_Roles";
            Fields = "RolesID, RolesName, RolesDescription";
            FieldsList = new string[] { 
                "RolesID, RolesName, RolesAuthID, AuthID, ModuleID, ModuleName, ModuleLink, ModuleMenu, ModuleTypeID, ModuleTypeName, FuncID, FuncName, FuncMenu"
            };
        }

        #region IRoles 成员

        /// <summary>
        /// 新增角色 
        /// </summary>
        /// <param name="info"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public int InsertRoles(RolesInfo info, out string message)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@RolesName",SqlDbType.NVarChar,20)
                ,new SqlParameter("@RolesDescription",SqlDbType.NVarChar,100)
                ,new SqlParameter("@RtnID",SqlDbType.Int)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
                ,new SqlParameter("@RtnMsg",SqlDbType.NVarChar,100)
            };
            int i = 0;
            parm[i++].Value = info.RolesName;
            parm[i++].Value = info.RolesDescription;
            parm[i++].Direction = ParameterDirection.Output;
            parm[i++].Direction = ParameterDirection.Output;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "AddRoles", parm);
            message = parm[parm.Length - 1].Value.ToString();
            if (parm[parm.Length - 2].Value.ToString().Equals("80"))
                return int.Parse(parm[parm.Length - 3].Value.ToString());
            return 0;
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="info"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool UpdateRoles(RolesInfo info, out string message)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@RolesID",SqlDbType.NVarChar,20)
                ,new SqlParameter("@RolesName",SqlDbType.NVarChar,20)
                ,new SqlParameter("@RolesDescription",SqlDbType.NVarChar,100)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
                ,new SqlParameter("@RtnMsg",SqlDbType.NVarChar,100)
            };
            int i = 0;
            parm[i++].Value = info.RolesID;
            parm[i++].Value = info.RolesName;
            parm[i++].Value = info.RolesDescription;
            parm[i++].Direction = ParameterDirection.Output;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "UpdateRoles", parm);
            message = parm[parm.Length - 1].Value.ToString();
            return parm[parm.Length - 2].Value.ToString().Equals("80");
        }

        public bool DeleteRoles(int rolesID)
        {
            throw new NotImplementedException();
        }

        public RolesInfo GetRolesByID(int id)
        {
            Sql = string.Format("SELECT TOP 1 {0} FROM {1} WHERE RolesID = {2}", Fields, Schema + TableName, id);
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, Sql);
            RolesInfo info = new RolesInfo();
            if (dr != null)
            {
                if (dr.Read())
                {
                    int i = 0;                    
                    info.RolesID = dr.GetInt32(i++);
                    info.RolesName = dr.GetString(i++);
                    info.RolesDescription = dr.GetString(i++);
                }
                dr.Close();
            }
            return info;
        }

        /// <summary>
        /// 角色列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public IList<RolesInfo> ListRoles(BaseCondition condition)
        {
            SqlBuilder = new StringBuilder("SELECT ");
            if (condition.Top > 0)
                SqlBuilder.AppendFormat(" TOP({0}) ", condition.Top);
            SqlBuilder.AppendFormat(" {0} FROM {1} WHERE 1 = 1 ", Fields, Schema + TableName);
            if (!string.IsNullOrEmpty(condition.OrderBy))
                SqlBuilder.AppendFormat(" ORDER BY {0}", condition.OrderBy);
            List<RolesInfo> list = new List<RolesInfo>();
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, SqlBuilder.ToString());
            if (dr != null)
            {
                while (dr.Read()) 
                {
                    int i = 0;
                    RolesInfo info = new RolesInfo();
                    info.RolesID = dr.GetInt32(i++);
                    info.RolesName = dr.GetString(i++);
                    info.RolesDescription = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }

        /// <summary>
        /// 角色权限列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public IList<RolesInfo> ListRolesAuth(RolesCondition condition)
        {//RolesID, RolesName, RolesAuthID, AuthID, ModuleID, ModuleName, ModuleLink, ModuleMenu, ModuleTypeID, ModuleTypeName, FuncID, FuncName, FuncMenu
            SqlBuilder = new StringBuilder("SELECT ");
            if (condition.Top > 0)
                SqlBuilder.AppendFormat(" TOP({0}) ", condition.Top);
            SqlBuilder.AppendFormat(" {0} FROM {1} WHERE 1 = 1 ", FieldsList[0], Schema + ViewTableName);
            if (condition.RolesID > 0)
                SqlBuilder.AppendFormat(" AND RolesID = {0}", condition.RolesID);
            if (!string.IsNullOrEmpty(condition.OrderBy))
                SqlBuilder.AppendFormat(" ORDER BY {0}", condition.OrderBy);
            List<RolesInfo> list = new List<RolesInfo>();
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, SqlBuilder.ToString());
            if (dr != null)
            {
                while (dr.Read())
                {
                    int i = 0;
                    RolesInfo info = new RolesInfo();
                    info.RolesID = dr.GetInt32(i++);
                    info.RolesName = dr.GetString(i++);

                    info.RolesAuth = new RolesAuthInfo();
                    info.RolesAuth.RolesAuthID = dr.GetInt32(i++);

                    info.RolesAuth.Auth = new AuthInfo();
                    info.RolesAuth.Auth.AuthID = dr.GetInt32(i++);

                    info.RolesAuth.Auth.Module = new ModuleInfo();
                    info.RolesAuth.Auth.Module.ModuleID = dr.GetInt32(i++);
                    info.RolesAuth.Auth.Module.ModuleName = dr.GetString(i++);
                    info.RolesAuth.Auth.Module.ModuleLink = dr.GetString(i++);
                    info.RolesAuth.Auth.Module.ModuleMenu = dr.GetString(i++);

                    info.RolesAuth.Auth.Module.ModuleType = new ModuleTypeInfo();
                    info.RolesAuth.Auth.Module.ModuleType.ModuleTypeID = dr.GetInt32(i++);
                    info.RolesAuth.Auth.Module.ModuleType.ModuleTypeName = dr.GetString(i++);

                    info.RolesAuth.Auth.Func = new FuncInfo();
                    info.RolesAuth.Auth.Func.FuncID = dr.GetInt32(i++);
                    info.RolesAuth.Auth.Func.FuncName = dr.GetString(i++);
                    info.RolesAuth.Auth.Func.FuncMenu = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }
        #endregion
    }
}
